package com.meixiang.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.account.AccountMessageList;
import com.meixiang.util.GlideHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 物流消息
 */
public class LogisticsMessageAdapter extends RecyclerView.Adapter<LogisticsMessageAdapter.MyViewHolder> {


    private Context context;
    public List<AccountMessageList> mList = new ArrayList<>();
    String mImgServer;

    public LogisticsMessageAdapter(Context context) {
        this.context = context;
    }

    public void addAll(List<AccountMessageList> mList,String imgServer) {
        this.mList.addAll(mList);
        this.mImgServer=imgServer;
        this.notifyDataSetChanged();
    }

    public void clearData() {
        this.mList.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_logistics_message, parent, false));
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            });
        }


        if (mList != null) {
            AccountMessageList mData = mList.get(position);
            GlideHelper.showImage(context, mImgServer+ mData.getMessageImage(), R.drawable.image_default_white_bg, holder.mIvPicture);
            holder.mTvMsgTitle.setText(mData.getTitle());
            holder.mTvMsgTime.setText(mData.getCreateTime());
            holder.mTvMsgContent.setText(mData.getTicker());
        }
//        if (position==1){
//            holder.mIvPicture.setImageResource(R.mipmap.pic1);
//            holder.mTvMsgTitle.setText("您购买的商品发货啦！");
//            holder.mTvMsgTime.setText("02月24日 06:55");
//            holder.mTvMsgContent.setText("您在美享购买的商品已经发货了，去看看详情记录。");
//        }


    }

    @Override
    public int getItemCount() {

        return mList.size();
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_picture)
        ImageView mIvPicture;
        @Bind(R.id.tv_msg_title)
        TextView mTvMsgTitle;
        @Bind(R.id.tv_msg_time)
        TextView mTvMsgTime;
        @Bind(R.id.tv_msg_content)
        TextView mTvMsgContent;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}